package com.jx.lcs.marketTemplate.web;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.jx.lcs.knowledge.web.KnowledgeInfoController;
import com.jx.lcs.marketTemplate.entity.MarketTemplate;
import com.jx.lcs.marketTemplate.service.MarketTemplateService;
import com.jx.lcs.util.common.GetProp;
import com.tianyu.jty.common.persistence.Page;
import com.tianyu.jty.common.persistence.PropertyFilter;
import com.tianyu.jty.common.web.BaseController;

/**
 * @author 陈新雷 2015年7月23日下午4:29:20
 *
 */
@Controller
@RequestMapping("marketTemp")
public class MarketTemplateController extends BaseController {
	private static final Logger logger = LoggerFactory
			.getLogger(KnowledgeInfoController.class);
	@Autowired
	private MarketTemplateService mts;

	/**
	 * 默认页面
	 */
	@RequestMapping(method = RequestMethod.GET)
	public String list() {
		return "marketTemp/marketTempList";
	}

	/**
	 * 得到全部数据
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "getAll", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> getAll(HttpServletRequest request) {
		setOrderBy("recordtime");
		setOrder("DESC");
		Page<MarketTemplate> page = getPage(request);
		List<PropertyFilter> filters = PropertyFilter
				.buildFromHttpRequest(request);
		page = mts.search(page, filters);
		return getEasyUIData(page);
	}

	/**
	 * 添加模板跳转
	 */
	@RequestMapping(value = "create", method = RequestMethod.GET)
	public String createForm(Model model) {
		model.addAttribute("mt", new MarketTemplate());
		model.addAttribute("action", "create");
		return "marketTemp/marketTempForm";
	}

	/**
	 * 增加模板
	 * 
	 * @param KnowledgeInfo
	 * @param model
	 */
	@RequestMapping(value = "create", method = RequestMethod.POST)
	@ResponseBody
	public String create(@RequestParam MultipartFile imgurl,
			@RequestParam MultipartFile simgurl, HttpServletRequest request,
			HttpServletResponse response) {
		String path = GetProp.getConfig("MARKET_TEMPLATE_IMG_UPLOAD_PATH");
		String spath = GetProp.getConfig("MARKET_TEMPLATE_SIMG_UPLOAD_PATH");
		// 保存
		try {
			String fileName = this.upload(imgurl, path);
			String sfileName = this.upload(simgurl, spath);
			String tempname = request.getParameter("tempname");
			String tempurl = request.getParameter("tempurl");
			String status = request.getParameter("status");
			MarketTemplate mt = new MarketTemplate();
			mt.setTempname(tempname);
			mt.setTempurl(tempurl);
			mt.setStatus(status);
			System.out.println("------------11-------------");
			mt.setPublishstatus("0");
			mt.setImgurl(GetProp.getConfig("MARKET_TEMPLATE_IMG_SERVER_URL")
					+ fileName);
			mt.setSimgurl(GetProp.getConfig("MARKET_TEMPLATE_SIMG_SERVER_URL")
					+ sfileName);
			
			System.out.println("-----------22--------------");
			mts.save(mt);
		} catch (Exception e) {
			System.out.println(e);
			logger.info("上传头像异常:", e);
			e.printStackTrace();
		}
		return "success";

	}

	/**
	 * 修改模板跳转
	 */
	@RequestMapping(value = "update/{id}", method = RequestMethod.GET)
	public String updateForm(@PathVariable("id") String id, Model model) {
		model.addAttribute("mt", mts.get(id));
		model.addAttribute("action", "update");
		return "marketTemp/marketTempForm";
	}

	/**
	 * 修改模板
	 */
	@RequestMapping(value = "update", method = RequestMethod.POST)
	@ResponseBody
	public String update(@RequestParam MultipartFile imgurl,
			@RequestParam MultipartFile simgurl, HttpServletRequest request,
			HttpServletResponse response) {
		String id = request.getParameter("id");
		String eimg = request.getParameter("eimg");
		String seimg = request.getParameter("seimg");
		String tempname = request.getParameter("tempname");
		String tempurl = request.getParameter("tempurl");
		String status = request.getParameter("status");
		MarketTemplate mt = mts.get(id);
		mt.setTempname(tempname);
		mt.setTempurl(tempurl);
		if (null != status && !"".equals(status))
			mt.setStatus(status);
		if (null == eimg || "".equals(eimg)) {
			String path = GetProp.getConfig("MARKET_TEMPLATE_IMG_UPLOAD_PATH");
			try {
				String fileName = this.upload(imgurl, path);
				mt.setImgurl(GetProp
						.getConfig("MARKET_TEMPLATE_IMG_SERVER_URL") + fileName);
			} catch (Exception e) {
				logger.info("上传头像异常:", e);
				e.printStackTrace();
			}
		}
		if (null == seimg || "".equals(seimg)) {
			String spath = GetProp
					.getConfig("MARKET_TEMPLATE_SIMG_UPLOAD_PATH");
			try {
				String sfileName = this.upload(simgurl, spath);
				mt.setSimgurl(GetProp
						.getConfig("MARKET_TEMPLATE_SIMG_SERVER_URL")
						+ sfileName);
			} catch (Exception e) {
				logger.info("上传头像异常:", e);
				e.printStackTrace();
			}
		}
		mts.save(mt);
		return "success";
	}

	/**
	 * 修改模板
	 */
	@RequestMapping(value = "modify/{id}")
	@ResponseBody
	public String modify(@PathVariable("id") String id) {
		MarketTemplate mt = mts.get(id);
		mt.setStatus("1");
		mts.save(mt);
		return "success";
	}

	/**
	 * 修改模板
	 */
	@RequestMapping(value = "publish/{id}")
	@ResponseBody
	public String publish(@PathVariable("id") String id) {
		MarketTemplate mt = mts.get(id);
		mt.setPublishstatus("1");
		mts.save(mt);
		return "success";
	}
}
